<template>
    <OAMain title="个人考勤">
        <el-card style="text-align: right">
            <el-button type="primary" @click="onShowDialog"><el-icon>
                    <Plus />
                </el-icon>发起考勤</el-button>
        </el-card>
        <el-card>
            <el-table :data="absents">
                <el-table-column prop="title" label="标题" width="180" />
                <el-table-column prop="absent_type.name" label="类型" width="180" />
                <el-table-column prop="requset_content" label="原因" />
                <el-table-column label="发起时间">
                    <template #default="scope">
                        {{ dateformat.stringFromDateTime(scope.row.create_time) }}
                    </template>
                </el-table-column>
                <el-table-column prop="start_date" label="开始日期" />
                <el-table-column prop="end_date" label="结束日期" />
                <el-table-column label="审核领导">
                    {{ responder_str }}
                </el-table-column>
                <el-table-column prop='response_content' label='审批意见' />
                <el-table-column label='审批时间'>
                    <template #default="scope">
                        <el-tag v-if="scope.row.status == 1" type="info">审核中</el-tag>
                        <el-tag v-else-if="scope.row.status == 2" type="success">已通过</el-tag>
                        <el-tag v-if="scope.row.status == 3" type="danger">与拒绝</el-tag>
                    </template>
                </el-table-column>


            </el-table>
            <template #footer>
                <!-- <!-- <el-pagination background layout="prev, pager, next" :total="pagination.total"
                    v-model:current-page="pagination.page" :page-size="1" />
                     -->
                <OAPagination :total="pagination.total" v-model="pagination.page"></OAPagination>
            </template>

        </el-card>
        <OADialog title="发起请假" v-model="dialogFormVisible" @submit="onSubmitAbsent" @cancel="dialogFormVisible = false">
            <el-form :model="absentForm" :rules="rules" ref="absentFormRef">
            <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                <el-input v-model="absentForm.title" autocomplete="off" />
            </el-form-item>
            <el-form-item label="请假类型" :label-width="formLabelWidth" prop="absent_type_id">
                <el-select v-model="absentForm.absent_type_id" placeholder="请选择以下类型">
                    <el-option v-for="item in absent_types" :label="item.name" :value="item.id" :key="item.name" />
                </el-select>
            </el-form-item>
            <el-form-item label="请假时间" :label-width="formLabelWidth" prop="date_range">
                <el-date-picker v-model="absentForm.date_range" type="daterange" range-separator="到"
                    start-placeholder="起始日期" end-placeholder="结束日期" format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="审批领导" :label-width="formLabelWidth" prop="leader">
                <el-input :value="responder_str" readonly="True" autocomplete="off" />
            </el-form-item>
            <el-form-item label="请假理由" :label-width="formLabelWidth" prop="request_content">
                <el-input type="textarea" v-model="absentForm.request_content" autocomplete="off" />
            </el-form-item>
        </el-form>
        </OADialog>
   
</OAMain>
</template>

<script setup>
import OAPageHeader from '@/components/OAPageHeader.vue';
import { ref, reactive, onMounted, computed, watch } from 'vue'
import absentHttp from '@/api/absentHttp';
import { ElMessage } from 'element-plus';
import dateformat from '@/utils/dateformat';
import OAMain from '@/components/OAMain.vue';
import OAPagination from '@/components/OAPagination.vue';
import OADialog from '@/components/OADialog.vue';
let absentFormRef = ref(null)
let responder = reactive({
    email: '',
    realname: ''
})
let pagination = reactive({
    page: 1,
    total: 1
})
let rules = reactive({
    title: {
        required: true,
        message: '请输入标题',
        trigger: 'blur'
    },
    absent_type_id: {
        required: true,
        message: '请选择请假类型',
        trigger: 'change'
    },
    date_range: {
        required: true,
        message: '请选择请假时间',
        trigger: 'change'
    },
    request_content: {
        required: true,
        message: '请输入请假理由',
        trigger: 'blur'
    }

})
let formLabelWidth = ref('120px')
let dialogFormVisible = ref(false)
let absent_types = ref([])
let absentForm = reactive({
    title: '',
    absent_type_id: '',
    date_range: [],
    request_content: ''
})
let absents = ref([])

const onShowDialog = () => {
    // 重置表单数据
    absentForm.title = ''
    absentForm.absent_type_id = ''
    absentForm.date_range = []
    absentForm.request_content = ''
    // 显示对话框
    dialogFormVisible.value = true
}
const onSubmitAbsent = () => {
    absentFormRef.value.validate(async (valid, fields) => {
        if (valid) {
            let data = {
                title: absentForm.title,
                absent_type_id: absentForm.absent_type_id,
                start_date: absentForm.date_range[0],
                end_date: absentForm.date_range[1],
                request_content: absentForm.request_content
            }
            try {
                let absent = await absentHttp.applyAbsent(data)
                ElMessage.success('发起成功')
                dialogFormVisible.value = false
                absents.value.unshift(absent)
            } catch (error) {
                console.log(error)
            }
        }
    })
}
const requestAbsent=async(page)=>{
    try {
        let absents_data = await absentHttp.getMtAbsent(pagination.page)
        let total = absents_data.count
        pagination.total = total
        let results = absents_data.results
        absents.value = results
    } catch (error) {
        console.log(error)
    }
}
watch(() => pagination.page, async (newVal, oldVal) => {
    console.log(newVal)
    requestAbsent(newVal)
})
let responder_str = computed(() => {
    if (responder.email) {
        return '[' + responder.email + ']' + responder.realname
    } else {
        return '无'
    }
})
onMounted(async () => {
    try {
        let absent_types_data = await absentHttp.getAbsentTypes()
        absent_types.value = absent_types_data
        let responder_data = await absentHttp.getResponder()
        Object.assign(responder, responder_data)
        requestAbsent(1)
    } catch (e) {
        ElMessage.error(e)
    }
})
</script>

<style scoped>

</style>